using System;
using System.Data;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Windows.Forms;

namespace Wantn.Share
{
	public sealed class DataCov
	{
		private static int int_0;

		[DllImport("gdi32.dll")]
		private static extern int GetDeviceCaps(IntPtr intptr_0, int int_1);

		public static double Millimeters2Pixels(double mm)
		{
			double num = 0.0;
			Image image = null;
			Graphics graphics = null;
			IntPtr intPtr = IntPtr.Zero;
			try
			{
				image = new Bitmap(1, 1);
				graphics = Graphics.FromImage(image);
				intPtr = graphics.GetHdc();
				int deviceCaps = GetDeviceCaps(intPtr, 4);
				int deviceCaps2 = GetDeviceCaps(intPtr, 8);
				return (double)deviceCaps2 / (double)deviceCaps * mm;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (graphics != null)
				{
					graphics.ReleaseHdc(intPtr);
					graphics.Dispose();
				}
				image?.Dispose();
			}
		}

		public static int smethod_0(int val)
		{
			return val * 15;
		}

		public static int smethod_1(int val)
		{
			return val * 20;
		}

		public static int Pixel2CharWidth256(int val)
		{
			if (int_0 == 0)
			{
				using (Bitmap image = new Bitmap(1, 1))
				{
					using (Font font = new Font("Arial", 9f))
					{
						using (Graphics dc = Graphics.FromImage(image))
						{
							int_0 = 256 / TextRenderer.MeasureText(dc, "0", font, Size.Empty, TextFormatFlags.NoPadding).Width;
						}
					}
				}
			}
			return val * int_0;
		}

		public static string Seconds2HHMMSSString(long seconds)
		{
			long num = seconds % 60L;
			long num2 = seconds / 60L % 60L;
			long num3 = seconds / 60L / 60L;
			return $"{num3}:{num2}:{num}";
		}

		public static int[] Ushort2Binary(ushort num)
		{
			int[] array = new int[16];
			int num2 = 0;
			for (int i = 0; i < 16; i++)
			{
				num2 = 15 - i;
				array[num2] = (num & (1 << num2));
				array[num2] >>= num2;
			}
			return array;
		}

		public static int[] Ushort2BinaryBigEndian(ushort num)
		{
			num = (ushort)((num << 8) | (num >> 8));
			return Ushort2Binary(num);
		}

		public static DataTable Enum2Datatable(Type source, string keyColumnName, string valColumnName)
		{
			try
			{
				DataTable dataTable = new DataTable();
				dataTable.Columns.Add(keyColumnName, typeof(int));
				dataTable.Columns.Add(valColumnName, typeof(string));
				string[] names = Enum.GetNames(source);
				foreach (string text in names)
				{
					dataTable.Rows.Add(Convert.ToInt32(Enum.Parse(source, text)), text);
				}
				return dataTable;
			}
			catch
			{
				return null;
			}
		}

		public static Array AdjustArrayIndex<T>(T[] srcArray, int startIdx)
		{
			Array array = Array.CreateInstance(typeof(T), new int[1]
			{
				srcArray.Length
			}, new int[1]
			{
				startIdx
			});
			for (int i = startIdx; i <= srcArray.Length; i++)
			{
				array.SetValue(srcArray[i - 1], i);
			}
			return array;
		}

		public static byte[] Struct2Binary<T>(T structIn) where T : struct
		{
			byte[] array = null;
			IntPtr intPtr = IntPtr.Zero;
			int num = 0;
			try
			{
				num = Marshal.SizeOf(structIn);
				intPtr = Marshal.AllocHGlobal(num);
				Marshal.StructureToPtr(structIn, intPtr, fDeleteOld: false);
				array = new byte[num];
				Marshal.Copy(intPtr, array, 0, num);
				return array;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (intPtr != IntPtr.Zero)
				{
					Marshal.FreeHGlobal(intPtr);
				}
			}
		}

		public static T Binary2Struct<T>(byte[] binIn) where T : struct
		{
			T val = default(T);
			IntPtr intPtr = IntPtr.Zero;
			int num = 0;
			try
			{
				num = Marshal.SizeOf(typeof(T));
				if (num > binIn.Length)
				{
					throw new Exception("源byte[]长度小于目标结构体的大小");
				}
				intPtr = Marshal.AllocHGlobal(num);
				Marshal.Copy(binIn, 0, intPtr, num);
				return (T)Marshal.PtrToStructure(intPtr, typeof(T));
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (intPtr != IntPtr.Zero)
				{
					Marshal.FreeHGlobal(intPtr);
				}
			}
		}

		public static byte[] Dataset2Binary(DataSet dsIn)
		{
			byte[] array = null;
			MemoryStream memoryStream = null;
			try
			{
				memoryStream = new MemoryStream();
				IFormatter formatter = new BinaryFormatter();
				dsIn.RemotingFormat = SerializationFormat.Binary;
				formatter.Serialize(memoryStream, dsIn);
				memoryStream.Seek(0L, SeekOrigin.Begin);
				return memoryStream.GetBuffer();
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (memoryStream != null)
				{
					memoryStream.Close();
					memoryStream.Dispose();
				}
			}
		}

		public static DataSet Binary2Dataset(byte[] binIn)
		{
			MemoryStream memoryStream = null;
			DataSet dataSet = null;
			try
			{
				memoryStream = new MemoryStream(binIn, writable: true);
				IFormatter formatter = new BinaryFormatter();
				return (DataSet)formatter.Deserialize(memoryStream);
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (memoryStream != null)
				{
					memoryStream.Close();
					memoryStream.Dispose();
				}
			}
		}

		public static byte[] Object2Binary(object objIn)
		{
			byte[] array = null;
			MemoryStream memoryStream = null;
			try
			{
				memoryStream = new MemoryStream();
				IFormatter formatter = new BinaryFormatter();
				formatter.Serialize(memoryStream, objIn);
				memoryStream.Seek(0L, SeekOrigin.Begin);
				return memoryStream.GetBuffer();
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (memoryStream != null)
				{
					memoryStream.Close();
					memoryStream.Dispose();
				}
			}
		}

		public static object Binary2Object(byte[] binIn)
		{
			object obj = null;
			MemoryStream memoryStream = null;
			try
			{
				memoryStream = new MemoryStream(binIn);
				IFormatter formatter = new BinaryFormatter();
				return formatter.Deserialize(memoryStream);
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (memoryStream != null)
				{
					memoryStream.Close();
					memoryStream.Dispose();
				}
			}
		}

		public static string Binary2HexString(byte[] bytes)
		{
			StringBuilder stringBuilder = new StringBuilder(bytes.Length);
			for (int i = 0; i < bytes.Length; i++)
			{
				stringBuilder.Append(bytes[i].ToString("X2"));
			}
			return stringBuilder.ToString();
		}

		public static byte[] HexString2Binary(string str)
		{
			byte[] array = new byte[str.Length / 2];
			for (int i = 0; i < array.Length; i++)
			{
				array[i] = (byte)(smethod_2(str.Substring(i * 2, 1)) * 16 + smethod_2(str.Substring(i * 2 + 1, 1)));
			}
			return array;
		}

		private static byte smethod_2(string string_0)
		{
			switch (string_0)
			{
			case "0":
				return 0;
			case "1":
				return 1;
			case "2":
				return 2;
			case "3":
				return 3;
			case "4":
				return 4;
			case "5":
				return 5;
			case "6":
				return 6;
			case "7":
				return 7;
			case "8":
				return 8;
			case "9":
				return 9;
			case "A":
				return 10;
			case "B":
				return 11;
			case "C":
				return 12;
			case "D":
				return 13;
			case "E":
				return 14;
			case "F":
				return 15;
			default:
				return 0;
			}
		}


	}
}
